package cn.moningxi.service.impl;

import cn.moningxi.bean.CustomerInfomation;
import cn.moningxi.mapper.CustomerInfomationMapper;
import cn.moningxi.service.CustomerInfomationService;
import cn.moningxi.util.CustomIdGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class CustomerInfomationServiceImpl implements CustomerInfomationService {
    @Autowired
    private CustomerInfomationMapper customerInfomationMapper;

    public CustomerInfomation queryByEmail(CustomerInfomation cu) {
        return customerInfomationMapper.queryByEmail(cu);
    }

    @Override
    public boolean register(CustomerInfomation cu) {
        CustomerInfomation customerInfomation = customerInfomationMapper.queryByEmail(cu);
        if (customerInfomation == null) {
            cu.setRegisterTime(LocalDateTime.now());
            cu.setCustomerId(String.valueOf(new CustomIdGenerator().nextId(CustomerInfomation.class)));
            customerInfomationMapper.register(cu);
            return true;
        } else {
            return false;
        }
    }

    @Override
    public List<CustomerInfomation> queryMembers() {
        return customerInfomationMapper.queryMembers();
    }

    @Override
    public List<CustomerInfomation> queryDelMembers() {
        return customerInfomationMapper.queryDelMembers();
    }

    @Override
    public void removeMember(Integer customerInfoId) {
        customerInfomationMapper.removeMember(customerInfoId);
    }

    @Override
    public void returnMember(Integer customerInfoId) {
        customerInfomationMapper.returnMember(customerInfoId);
    }

    @Override
    public CustomerInfomation queryUser(CustomerInfomation loginUser) {
        System.out.println(loginUser.getCustomerId());
        return customerInfomationMapper.queryUser(loginUser);
    }

    @Override
    public CustomerInfomation queryUserById(int id) {
        return customerInfomationMapper.queryUserByID(id);
    }

    @Override
    public void editMembers(Integer customerInfoId, String customerStatus) {
        if ("1".equals(customerStatus)) {//账号状态=1，即账号正常，
            customerInfomationMapper.freeze(customerInfoId);//将该账号冻结
        } else {//账号状态≠1，即账号已被冻结，
            customerInfomationMapper.unfreeze(customerInfoId);//将该账号解冻
        }
    }

    @Override
    public boolean updateUser(CustomerInfomation loginUser) {
        System.out.println(loginUser.getCustomerId());
        loginUser.setModifyTime(LocalDateTime.now());
        return customerInfomationMapper.updateUser(loginUser);
    }

    @Override
    public CustomerInfomation queryUserByEmail(CustomerInfomation loginUser) {
        return customerInfomationMapper.queryUserByEmail(loginUser);
    }

    @Override
    public CustomerInfomation queryUserByPhone(CustomerInfomation loginUser) {
        return customerInfomationMapper.queryUserByPhone(loginUser);
    }

    @Override
    public List<CustomerInfomation> selectCustomerLevel() {
        return customerInfomationMapper.selectCustomerLevel();
    }

    @Override
    public void updateBalance(CustomerInfomation onlineUser) {
        customerInfomationMapper.updateBalance(onlineUser);
    }

    @Override
    public boolean updateUserPwd(String customerInfoId, String oldp, String newp) {
        return customerInfomationMapper.updateUserPwd(customerInfoId, oldp, newp);

    }

    @Override
    public void updateUserheadUrl(String customerId, String headUrl) {
        customerInfomationMapper.updateUserheadUrl(customerId, headUrl);
    }
}
